package com.yanggeng.demo.beginnerguidedemo.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import com.amap.api.maps.model.LatLng;
import com.yanggeng.demo.beginnerguidedemo.TestApplication;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanggeng on 2017/9/22.
 */

public class PackageManagerUtil {

    private static PackageManager mPackageManager = TestApplication.getApplication().getPackageManager();
    private static List<String> mPackageNames = new ArrayList<>();
    private static final String GAODE_PACKAGE_NAME = "com.autonavi.minimap";
    private static final String BAIDU_PACKAGE_NAME = "com.baidu.BaiduMap";


    private static void initPackageManager(){

        List<PackageInfo> packageInfos = mPackageManager.getInstalledPackages(0);

        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                mPackageNames.add(packageInfos.get(i).packageName);
            }
        }
    }

    public static boolean haveAMap(){
        initPackageManager();
        return mPackageNames.contains(GAODE_PACKAGE_NAME);
    }

    public static boolean haveBMap(){
        initPackageManager();
        return mPackageNames.contains(BAIDU_PACKAGE_NAME);
    }

    public static void toMapApp(Context context, double lat, double lon, String name) {
        Uri uri = Uri.parse("geo:" + lat + "," + lon + "," + name);
        context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }
}
